package com.wsg.lightaccounting.Activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wsg.lightaccounting.Model.AccountManager;
import com.wsg.lightaccounting.Model.CashCard;
import com.wsg.lightaccounting.PopupWindow.EditPopupWindow;
import com.wsg.lightaccounting.R;

public class CashCardActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView back,check;
    private LinearLayout bank,name,card_id;
    private TextView bank_text,name_text,card_id_text;
    private boolean isEdit = false;
    private long id = -1L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cash_card);
        Bundle bundle = this.getIntent().getExtras();
        isEdit = bundle.getBoolean("isEdit");
        intiView();
    }
    private void intiView(){
        back = (ImageView) findViewById(R.id.back);
        check = (ImageView) findViewById(R.id.check);

        bank = (LinearLayout) findViewById(R.id.bank_choose);
        card_id = (LinearLayout) findViewById(R.id.card_id_add);
        name = (LinearLayout) findViewById(R.id.name_add);

        bank_text = (TextView) findViewById(R.id.bank_name);
        card_id_text = (TextView) findViewById(R.id.card_id);
        name_text = (TextView) findViewById(R.id.name_wallet);

        back.setOnClickListener(this);
        check.setOnClickListener(this);

        bank.setOnClickListener(this);
        card_id.setOnClickListener(this);
        name.setOnClickListener(this);

        if (isEdit){
            Bundle bundle = this.getIntent().getExtras();
            bank_text.setText(bundle.getString("bank_name"));
            card_id_text.setText(bundle.getString("card_id"));
            name_text.setText(bundle.getString("name"));
            id = bundle.getLong("id");
            TextView textView = (TextView) findViewById(R.id.title);
            textView.setText("编辑储蓄卡");
        }
    }

    private void showEditPopupWinddow(TextView textView,int i){
        EditPopupWindow editPopupWindow = new EditPopupWindow(this,this.findViewById(R.id.mark_size).getWidth(),
                this.findViewById(R.id.mark_size).getHeight(),textView,i);
        editPopupWindow.showAtLocation(this.findViewById(R.id.mark_size), Gravity.BOTTOM,0,0);
    }

    public static void changeInput(EditText editText,Context context, boolean isshow){
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (isshow) {
            imm.showSoftInput(editText,InputMethodManager.SHOW_FORCED);
        }
        else if (!isshow){
            imm.hideSoftInputFromInputMethod(editText.getWindowToken(),0);
        }
       // imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.check:
                if (!isEdit){
                AccountManager.getAccountManager(this).addCashCard(new CashCard(0L,String.valueOf(card_id_text.getText()),
                        String.valueOf(bank_text.getText()),String.valueOf(name_text.getText()),0L));}
                else{
                    AccountManager.getAccountManager(this).updateCashCard(id,String.valueOf(bank_text.getText()),
                            String.valueOf(card_id_text.getText()),String.valueOf(name_text.getText()));
                }
                MainActivity.updateAllUI(this);
                finish();
                break;
            case R.id.bank_choose:
                showEditPopupWinddow(bank_text,EditPopupWindow.INPUT_LIMIT_NONE);
                break;
            case R.id.card_id_add:
                showEditPopupWinddow(card_id_text,EditPopupWindow.INPUT_LIMIT_NUMBER);
                break;
            case R.id.name_add:
                showEditPopupWinddow(name_text,EditPopupWindow.INPUT_LIMIT_NONE);
                break;
            default:
                break;
        }
    }
}
